package com.bier.fishproject.activity;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TMultiplexedProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.json.JSONObject;

import com.bier.fishproject.R;
import com.bier.fishproject.utils.Base64Str;
import com.bier.fishproject.utils.Constant;
import com.bier.fishproject.utils.MyThreadPool;
import com.bier.fishproject.utils.Shared;
import com.bier.fishproject.utils.YLWWater;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
 * 卡片详情
 * @author Administrator
 *
 */
public class WaterDetailsActivity extends BaseActivity{

	private LinearLayout lin;
	private TextView tvStream,tvYCode,tvZCode,tvUserName,tvStatus,tvCode,tvName,tvReckon,tvStartTime,tvSecond,tvAt,tvPrice,tvNowAt,tvApply,tvMoney,tvHandle,tvPeiceAt;
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_water_details);  
		initView();
	}
	
	/**
	 * 初始化方法
	 */
	private void initView(){
		lin=(LinearLayout)findViewById(R.id.lin_aw);
		tvStream=(TextView)findViewById(R.id.tv_awd_stream);
		tvYCode=(TextView)findViewById(R.id.tv_awd_ycode);
		tvZCode=(TextView)findViewById(R.id.tv_awd_zcode);
		tvUserName=(TextView)findViewById(R.id.tv_awd_userName);
		tvStatus=(TextView)findViewById(R.id.tv_wi_status);
		tvCode=(TextView)findViewById(R.id.tv_wi_code);
		tvName=(TextView)findViewById(R.id.tv_wi_name);
		tvReckon=(TextView)findViewById(R.id.tv_wi_reckon);
		tvStartTime=(TextView)findViewById(R.id.tv_wi_startTime);
		tvSecond=(TextView)findViewById(R.id.tv_wi_second);
		tvAt=(TextView)findViewById(R.id.tv_wi_at);
		tvPrice=(TextView)findViewById(R.id.tv_wi_price);
		tvNowAt=(TextView)findViewById(R.id.tv_wi_nowat);
		tvApply=(TextView)findViewById(R.id.tv_wi_apply);
		tvMoney=(TextView)findViewById(R.id.tv_wi_moneys);
		tvHandle=(TextView)findViewById(R.id.tv_wi_handle);
		tvPeiceAt=(TextView)findViewById(R.id.tv_wi_priceAt);
		Bundle bundle=getIntent().getExtras();
		if(bundle!=null){
			final String result=bundle.getString("result");
			if(!TextUtils.isEmpty(result)){
				loadView("数据加载中...");
				MyThreadPool.getInstent().submit(new Runnable() {
					public void run() {
						try {
							TTransport transport = new TSocket(Constant.IP, Constant.PORT, 5000);
							transport.open();
							TProtocol protocol = new TBinaryProtocol(transport);
							TMultiplexedProtocol mp1 = new TMultiplexedProtocol(protocol, "YLWWater");
							YLWWater.Client client = new YLWWater.Client(mp1);
							String date=client.reqDevInfo(Shared.readString(getApplicationContext(), "code"),result);
							transport.close();
							if(!TextUtils.isEmpty(date)){
								final Message message=new Message();
								message.what=Constant.ONE;
								message.obj=date;
								handler.sendMessage(message);
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				 });
			}
		}
		//返回
		findViewById(R.id.img_ac_next).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				WaterDetailsActivity.this.finish();
			}
		});
	}
	
	
	@SuppressLint("HandlerLeak") private Handler handler=new Handler(){
		public void handleMessage(Message msg) {
			endView();
			String result=msg.obj.toString();
			Log.v("tag", result);
			if(msg.what==Constant.ONE){
				try{
					JSONObject json=new JSONObject(result);
					if(json.getInt("status")==0){
						lin.setVisibility(View.VISIBLE);
						JSONObject json2=new JSONObject(json.getString("infos"));
						tvStream.setText("流水号："+json2.getString("saleno"));
//						tvYCode.setText("使用卡号："+json2.getString("")==null ? "" : json2.getString("saleno"));
//						tvZCode.setText("使用卡号："+json2.getString("")==null ? "" : json2.getString("saleno"));
//						tvUserName.setText("卡主姓名："+Base64Str.getFromBase64(json2.getString("username")));
						if(json2.getString("dstatus").equals("0")){
							tvStatus.setText("水表状态：正常");
						}else{
							tvStatus.setText("水表状态：异常");
						}
						tvCode.setText("水表编号："+json2.getString("did"));
						tvName.setText("水表名称："+Base64Str.getFromBase64(json2.getString("dname")));
						if(json2.getString("ptype").equals("0")){
							tvReckon.setText("计时");
						}else if(json2.getString("ptype").equals("1")){
							tvReckon.setText("计量");
						}else if(json2.getString("ptype").equals("2")){
							tvReckon.setText("智能");
						}
						if(!json2.getString("stime").equals("0")){
							tvStartTime.setText(json2.getString("stime").substring(14, json2.getString("stime").length()));
						}else{
							tvStartTime.setText("0");
						}
						tvSecond.setText(json2.getString("utime"));
						tvAt.setText(json2.getString("stone"));
						if(json2.getString("ptype").equals("0")){
							tvPrice.setText(json2.getString("psecond"));
							tvPeiceAt.setText(R.string.priceAts);
						}else if(json2.getString("ptype").equals("1")){
							tvPrice.setText(json2.getString("ptone"));
							tvPeiceAt.setText(R.string.priceAt);
						}
						tvNowAt.setText(json2.getString("dreading"));
						tvApply.setText(json2.getString("utone"));
						tvMoney.setText(json2.getString("umoney"));
					}else{
						showShortToast(Base64Str.getFromBase64(json.getString("msg")));
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		};
	};
}
